import { Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common'
import { UploadService } from './upload.service'
import { fileMimetypeFilter, upload } from '@/decorators'
import { ConfigService } from '@nestjs/config'
import { FileInterceptor } from '@nestjs/platform-express'

@Controller('upload')
export class UploadController {
    constructor(private config: ConfigService, private UploadService: UploadService) {}

    @Post()
    @upload('file', {
        fileFilter: fileMimetypeFilter(
            'image',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        ),
    })
    upload(@UploadedFile() file: Express.Multer.File) {
        return {
            url: `${this.config.get(`IMAGE_URL`)}/${file.filename}`,
        }
    }

    @Post('qiniuyun')
    @UseInterceptors(FileInterceptor('file'))
    async uploadFile(@UploadedFile() file: Express.Multer.File) {
        const key = file.originalname
        const localFilePath = file.path
        const uploadedFileKey = await this.UploadService.uploadFile(localFilePath, key)

        // 处理上传成功后的逻辑
        // 返回上传成功的文件键值
        return { url: uploadedFileKey }
    }
}
